﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataBaseLibrary.DBInterfaces;
using System.ComponentModel;

namespace DataBaseLibrary.DBObjectsFB
{
    public class TechnicalCharacteristics : DBFObject, IDBObject, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }

        String m_ModelDriver = "";
        public string ModelDriver
        {
            get
            {
                return m_ModelDriver;
            }
            set
            {
                m_ModelDriver = value;
                OnPropertyChanged(new PropertyChangedEventArgs("ModelDriver"));
            }
        }

        String m_NumberDriver = "";
        public string NumberDriver
        {
            get
            {
                return m_NumberDriver;
            }
            set
            {
                m_NumberDriver = value;
                OnPropertyChanged(new PropertyChangedEventArgs("NumberDriver"));
            }
        }

        String m_NumberChassis = "";
        public string NumberChassis
        {
            get
            {
                return m_NumberChassis;
            }
            set
            {
                m_NumberChassis = value;
                OnPropertyChanged(new PropertyChangedEventArgs("NumberChassis"));
            }
        }

        String m_NumberCarcass = "";
        public string NumberCarcass
        {
            get
            {
                return m_NumberCarcass;
            }
            set
            {
                m_NumberCarcass = value;
                OnPropertyChanged(new PropertyChangedEventArgs("NumberCarcass"));
            }
        }

        double m_AllowableWeight = 0;
        public double AllowableWeight //Разрешённая масса
        {
            get
            {
                return m_AllowableWeight;
            }
            set
            {
                m_AllowableWeight = value;
                OnPropertyChanged(new PropertyChangedEventArgs("AllowableWeight"));
            }
        }

        double m_UnladenMass = 0;
        /// <summary>
        /// Масса без нагрузки
        /// </summary>
        public double UnladenMass 
        {
            get
            {
                return m_UnladenMass;
            }
            set
            {
                m_UnladenMass = value;
                OnPropertyChanged(new PropertyChangedEventArgs("UnladenMass"));
            }
        }

        String m_Color = "";
        public string Color
        {
            get
            {
                return m_Color;
            }
            set
            {
                m_Color = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Color"));
            }
        }

        double m_PowerDriver = 0;
        /// <summary>
        /// Мощность двигателя (л.с.)
        /// </summary>
        public double PowerDriver
        {
            get
            {
                return m_PowerDriver;
            }
            set
            {
                m_PowerDriver = value;
                OnPropertyChanged(new PropertyChangedEventArgs("PowerDriver"));
            }
        }

        double m_EngineCapacity = 0;
        public double EngineCapacity
        {
            get
            {
                return m_EngineCapacity;
            }
            set
            {
                m_EngineCapacity = value;
                OnPropertyChanged(new PropertyChangedEventArgs("EngineCapacity"));
            }
        }

        String m_TypeDriver = "";
        public string TypeDriver
        {
            get
            {
                return m_TypeDriver;
            }
            set
            {
                m_TypeDriver = value;
                OnPropertyChanged(new PropertyChangedEventArgs("TypeDriver"));
            }
        }


        public TechnicalCharacteristics()
        {
            NameTable = "TECHNICAL_CHARACTERISTICS";

            ModelDriver = "";
            NumberDriver = "";
            NumberChassis = "";
            NumberCarcass = "";
            Color = "";
            TypeDriver = "";
        }

        private void CreateData()
        {
            Parameters = new Dictionary<string, object>();
            Parameters.Add("MODEL_DRIVER", ModelDriver);
            Parameters.Add("NUMBER_DRIVER", NumberDriver);
            Parameters.Add("NUMBER_CHASSIS", NumberChassis);
            Parameters.Add("NUMBER_CARCASS", NumberCarcass);
            Parameters.Add("ALLOWABLE_WEIGHT", AllowableWeight);
            Parameters.Add("UNLADEN_MASS", UnladenMass);
            Parameters.Add("COLOR", Color);
            Parameters.Add("POWER_DRIVER", PowerDriver);
            Parameters.Add("ENGINE_CAPACITY", EngineCapacity);
            Parameters.Add("TYPE_DRIVER", TypeDriver);


        }

        object IDBObject.Save()
        {
            try
            {
                CreateData();
                return SaveObject();
            }
            catch (SystemException)
            {
                throw new SystemException(String.Format("Ошибка при сохранении объекта {0} в БД", this.ToString()));
            }
        }

        void IDBObject.Load(object ID)
        {
            try
            {
                CreateData();
                LoadObject(ID);

                ModelDriver = Parameters["MODEL_DRIVER"].ToString();
                NumberDriver = Parameters["NUMBER_DRIVER"].ToString();
                NumberChassis = Parameters["NUMBER_CHASSIS"].ToString();
                NumberCarcass = Parameters["NUMBER_CARCASS"].ToString();
                AllowableWeight = Convert.ToDouble(Parameters["ALLOWABLE_WEIGHT"]);
                UnladenMass = Convert.ToDouble(Parameters["UNLADEN_MASS"]);
                Color = Parameters["COLOR"].ToString();
                PowerDriver = Convert.ToDouble(Parameters["POWER_DRIVER"]);
                EngineCapacity = Convert.ToDouble(Parameters["ENGINE_CAPACITY"]);
                TypeDriver = Parameters["TYPE_DRIVER"].ToString();

            }
            catch (SystemException)
            {
                throw new SystemException(String.Format("Ошибка при загрузке объекта {0} в БД", this.ToString()));
            }
        }


        void IDBObject.Update()
        {
            CreateData();
            UpdateObject();
        }
    }
}
